<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .big {
            width: 1000px;
            position: absolute;
            left: 0;
            right: 0;
            margin: auto;
        }

        thead td {
            width: 200px;
            height: 50px;
        }

        thead tr {
            background-color: #fafafa;
        }

        tbody tr td img {
            width: 100px;
            height: 100px;
        }

        tfoot td {
            height: 50px;
        }

        .num {
            width: 40px;
            height: 20px;
            padding-left: 15px;
            border: 0;
            border-top: 1px solid #999;
            border-bottom: 1px solid #999;
        }

        .jia,
        .jian {
            width: 20px;
            height: 20px;
        }

        .sc {
            width: 40px;
            height: 25px;
            background-color: red;
            color: #fff;
            border: 0;
        }

        tfoot tr td span {
            font-size: 25px;
            margin-right: 15px;
            color: #ff69b4;
        }

        tfoot tr td button {
            width: 70px;
            height: 30px;
            background-color: #3f85ed;
            color: #fff;
            border: 0;
        }
    </style>
</head>

<body>
    <div class="big">
        <p><span class="iconfont icon-xiangsufeng_fangzi"></span>/<span>购物车</span></p>
        <table cellspacing="0">
            <thead align="center">
                <tr>
                    <td>选中</td>
                    <td>图片</td>
                    <td>单价</td>
                    <td>个数</td>
                    <td>小计</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody align="center">

            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4" style="padding-left: 75px;"><input type="checkbox" class="qx">&emsp13;全选</td>
                    <td colspan="2" align="center">总价: <b>¥<span class="price">42.00</span></b> <button>结算(<a
                                class="zsnum">12</a>)</button></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div class="empty" style="display: none;">🛒空空如也</div>
</body>

</html>
<script>
    const dataArr = [
        {
            id: 1,
            icon: './img/火龙果.png',
            isChecked: true,
            num: 2,
            price: 6,
        },
        {
            id: 2,
            icon: './img/樱桃.png',
            isChecked: false,
            num: 7,
            price: 20,
        },
        {
            id: 3,
            icon: './img/榴莲.png',
            isChecked: false,
            num: 3,
            price: 40,
        },
        {
            id: 4,
            icon: './img/荔枝.png',
            isChecked: true,
            num: 10,
            price: 3,
        },
        {
            id: 5,
            icon: './img/鸭梨.png',
            isChecked: false,
            num: 20,
            price: 34,
        },
    ]
    var tbody = document.querySelector('tbody')
    var price = document.querySelector('.price')
    var num = document.querySelector('.zsnum')
    var empty=document.querySelector('.empty')
    var qx=document.querySelector('.qx')
    function xr() {
        var arr = dataArr.map(function (ele, index) {
            return `
            <tr data-id=${index}>
                <td><input type="checkbox" class='fxk' ${ele.isChecked==true?'checked':''} ></td>
                <td><img src="${ele.icon}" alt=""></td>
                <td>${ele.price}</td>
                <td><button class="jian">-</button><input class="num" type="text" value="${ele.num}"><button class="jia" >+</button></td>
                <td>${ele.price*ele.num}</td>
                <td><button class="sc">删除</button></td>
            </tr>
            `
        })
        //渲染页面
        tbody.innerHTML=arr.join('')
        //反选
        qx.checked=dataArr.every(ele=>ele.isChecked==true)
        //获取选中的数据
        var brr=dataArr.filter(ele=>ele.isChecked==true)
        //赋值总价
        price.innerHTML=brr.reduce((prve,ele)=>prve+=ele.num*ele.price,0).toFixed(2)
        //赋值总数
        num.innerHTML=brr.reduce((prve,ele)=>prve+=ele.num,0)
        //空空如也显示
        empty.style.display= dataArr==''?'block':'none'
    }
    xr()
    tbody.addEventListener('click',function(e){
        var id=e.target.parentNode.parentNode.dataset.id
        console.log(id);
        
        if(e.target.className=='sc'){
            dataArr.splice(id,1)
            xr()
        }

        if(e.target.tagName=='INPUT'){
            dataArr[id].isChecked=e.target.checked
            xr()
        }

        if(e.target.className=='jia'){
            dataArr[id].num++
            xr()
        }

        if(e.target.className=='jian'){
            if(dataArr[id].num<=1){
                dataArr[id].num=1
            }else{
                dataArr[id].num--
                xr()
            }
        }
    })
    qx.addEventListener('click',function(){
        dataArr.filter(ele=>ele.isChecked=this.checked)
        xr()
    })
</script>